"""
制作词云
"""
from collections import Counter

import bs4
import jieba
import numpy
import requests
import wordcloud

from PIL import Image
from matplotlib import pyplot as plt

empty_words = {
    '你', '我', '他', '的', '了', '啊', '，', '。', '…', '是',
    '和', '要', '也', '有', '都', '不', '、', '；', '会', ' ',
    '我们', '你们', '他们', '再', '上', '下', '左', '又', '在',
    '还是', '就', '不能', '妳', '她', '这', '那', '一个', '走',
    '很', '最', '一条', '一只', '只是', '那么', '这么', '可能',
    '而', '而且', '或', '或者', '又', '又是', '有些', '多', '少',
    '来', '去', '？', '就是', '被', '才', '既', '既然', '着',
    '即使', '里', '外', '“', '”', '不是', '什么', '之', '把',
    '说', '\n', '！', '给', '让', '对', '一部', '想', '用', '大',
    '小', '吧', '但是', '（', '）', '.', '^', '男', '女', '但',
    '能够', '儿', '一定', '已经', '只有', '个', '岁', '谁', '过',
    '却', '之间', '对应', '连', '看', '人', '自己', '没有', '很多',
    '—', '真的', '觉得', '帮', '还有', '应该', '再也', '能', '否则',
    '只', '因为', '所以', '记', '算不上', '在我看来', '与', '曾经'
}


def crawl_comments(url):
    comments_words = []
    for start in range(20):
        resp = requests.get(
            url=f'{url}?start={start}&offset=20',
            headers={
                'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) '
                              'AppleWebKit/537.36 (KHTML, like Gecko) '
                              'Chrome/83.0.4103.97 Safari/537.36',
                'Accept': 'text/html,application/xhtml+xml,application/xml;'
                          'q=0.9,image/webp,image/apng,*/*;'
                          'q=0.8,application/signed-exchange;v=b3;q=0.9',
                'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
            }
        )
        soup = bs4.BeautifulSoup(resp.text, 'lxml')
        comment_spans = soup.select('div.comment-item>div.comment>p>span.short')
        for span in comment_spans:
            for word in jieba.cut(span.text):
                if word not in empty_words:
                    comments_words.append(word)
    return comments_words


def main():
    comments_words = crawl_comments(
        'https://movie.douban.com/subject/1291561/comments'
    )
    counter = Counter(comments_words)
    common_words = [word for word, _ in counter.most_common(100)]
    print(common_words)

    mask_image = Image.open('res/alice_mask.png')
    width, height = mask_image.size
    mask = numpy.array(mask_image)
    wc = wordcloud.WordCloud(
        font_path='res/ArialUnicode.ttf',
        width=width, height=height,
        background_color='white',
        # mask=mask,
        collocations=False
    )
    wc.generate(' '.join(common_words))
    plt.imshow(wc, interpolation='catrom')
    plt.axis('off')
    plt.show()


if __name__ == '__main__':
    main()
